package com.huiyujiaoyu.util;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.widget.GlideCircleTransform;
import com.huiyujiaoyu.widget.GlideRoundTransform;


/**
 * Created by baixiaokang on 16/5/6.
 */
public class GlideUtil {
    private final static int DEFAULT_PIC = R.mipmap.ic_launcher;

    private final static int DEFAULT_AVATAR_PIC = R.mipmap.ic_launcher;
    public static void loadImg(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(DEFAULT_PIC)
                .placeholder(DEFAULT_PIC)
                .into(v);
    }
    public static void loadImg2(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(v);
    }
    public static void loadImg(ImageView v, int id) {
        Glide.with(v.getContext())
                .load(id)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(v);
    }

    public static void loadImg(ImageView v, byte[] bytes) {
        Glide.with(v.getContext())
                .load(bytes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(v);
    }

    public static void loadRoundImg(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(v.getContext()))
                .error(DEFAULT_PIC)
                .into(v);
    }


    public static void loadRoundImg(ImageView v, int id) {
        Glide.with(v.getContext())
                .load(id)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(v.getContext()))
                .error(DEFAULT_PIC)
                .into(v);
    }



    public static void loadCircleImg(ImageView v, int id) {
        Glide.with(v.getContext())
                .load(id)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransform(v.getContext()))
                .error(DEFAULT_AVATAR_PIC)
                .into(v);
    }

    public static void loadCircleImg(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransform(v.getContext()))
                .error(DEFAULT_AVATAR_PIC)
                .into(v);
    }
    /**
     * 加载gif
     * @param imageView
     * @param imgUrl
     */
    public void loadGifImage(ImageView imageView, String imgUrl) {
        Glide.with(imageView.getContext())
                .load(imgUrl)
                .asGif()
                .crossFade()
                .priority(Priority.NORMAL) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .error(DEFAULT_PIC)
                .dontAnimate().into(imageView);
    }
    public static void loadHeaderImg(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransform(v.getContext()))
                .error(DEFAULT_AVATAR_PIC)
                .placeholder(DEFAULT_AVATAR_PIC)
                .into(v);
    }

    public void loadRoundImage(ImageView imageView, String url, int defaultImage) {
        Glide.with(imageView.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(defaultImage)
                .transform(new GlideRoundTransform(imageView.getContext()))
                .placeholder(defaultImage)
                .into(imageView);
    }

    public static void loadRoundAndBgImg(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransform(v.getContext()))
                .error(DEFAULT_PIC)
                .into(v);
    }

    /**
     * 加载图片并设置为指定大小
     * @param imageView
     * @param imgUrl
     * @param withSize
     * @param heightSize
     */
    public static  void loadOverrideImage(ImageView imageView,
                                  String imgUrl, int withSize, int heightSize) {
        Glide.with(imageView.getContext())
                .load(imgUrl)
                .error(DEFAULT_PIC)
                .crossFade()
                .priority(Priority.NORMAL) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .override(withSize, heightSize).centerCrop()
                .dontAnimate().into(imageView);
    }
    public static  void loadOverrideImage(ImageView imageView,
                                          int rid, int withSize, int heightSize) {
        Glide.with(imageView.getContext())
                .load(rid)
                .error(DEFAULT_PIC)
                .crossFade()
                .priority(Priority.NORMAL) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .override(withSize, heightSize)
                .dontAnimate().into(imageView);
    }

    /**
     * 恢复请求，一般在停止滚动的时候
     * @param context
     */
    public static void resumeRequests(Context context) {
        Glide.with(context).resumeRequests();
    }

    /**
     * 暂停请求 正在滚动的时候
     * @param context
     */
    public static void pauseRequests(Context context) {
        Glide.with(context).pauseRequests();
    }


}
